/*************************************************
Author: tony
Date: 11/26/2012
Description:
Notes:
*************************************************/

#ifndef __CDrugMgr_h__
#define __CDrugMgr_h__

#include "CTArray.h"
#include "commclass.h"
#include "CDrugConfig.h"
#include "../common/CRandom.h"
typedef CTArray<CAttrPair, MAX_DRUG_TYPE> T_DRUG_ATTR_LIST;

class CPlayerOnlineData;

class MsgAnsDrugMakeInfo;
class MsgAnsRoleDrugInfo;
class MsgReqDrugMake;
class MsgReqDrugInherit;
class MsgAnsDrugInherit;
class MsgInheritInfo;
class MsgAnsDrugMake;
class CDrugMgr
{
public:
	CDrugMgr()
    {
	}

	virtual ~CDrugMgr()
    {
	}

    int Init(const char * sBuff);
    int CountSize();

    int LoadConfig(const char* szLogPath);

    int CheckRoleId (CPlayerDetailInfo& stPlayerDetailInfo, unsigned short usRoleId);
    CDrugData & GetUserDrug(CPlayerDetailInfo& stPlayerDetailInfo, unsigned short usRoleId);

    //是否为有效的DrugId
    bool IsValidDrug (unsigned short usDrugId);

    //获取魔药级别
    int GetDrugLevel (unsigned short usDrugId);

    //获取魔药属性类型
    int GetAttrType (unsigned short usDrugId);

    //获取某瓶魔药
    int GetSingleDrug (CDrugData & stUserDrug, unsigned short usDrugId, CSingleDrug & stSingleDrug);
    CSingleDrug & GetSingleDrug(CDrugData & stUserDrug, unsigned short usDrugId);

    //获取魔药容量
    int GetCapacityPercent (CDrugData & stSrcDrug, unsigned short usDrugId);

    //获取魔药属性类型
    int GetDrugAttr (CPlayerDetailInfo& stPlayerDetailInfo, unsigned short usRoleId, CPlayerAttr & stPlayerAttr);
    int GetDrugTotalAttr (CDrugData & stUserDrug, CPlayerAttr & stTotalAttr);
    int GetSingleAttr (unsigned short usDrugId, CSingleDrug & stSingleDrug, CAttrPair & stAttr);
    int GetAttrOfSameType (CDrugData & stUserDrug, unsigned char ucAttrType);
   
    CDrugConfigData & GetDrugConfigData(unsigned short usDrugId);

    //获取主角的魔药信息
    int GetRoleDrugInfo(CPlayerDetailInfo& stPlayerDetailInfo, unsigned short usRoldId, MsgAnsRoleDrugInfo & stAns);


    int GetSyntheDrugCfg(CDrugOperKey &stKey, CSyntheDrugConfig & stConfigData);
    int GetSyntheNextCfg (unsigned short usDrugId, CSingleDrug & stSingleDrug, CSyntheDrugConfig & stSynConfigData);

    int GetPurifyDrugCfg (CDrugOperKey &stKey, CPurifyDrugConfig & stConfigData);
    int GetPurifyDrugConfig (unsigned char ucLevel, CSingleDrug & stSingleDrug, CPurifyDrugConfig  & stConfigData);


    int SynthetizeDrug (CPlayerBaseInfo & stPlayerBaseInfo, CPlayerDetailInfo& stPlayerDetailInfo, CPlayerOnlineData & stPlayerOnlineData, 
                        MsgReqDrugMake & stReq, MsgAnsDrugMake & stAns);

    int PurifyDrug (CPlayerBaseInfo & stPlayerBaseInfo, CPlayerDetailInfo& stPlayerDetailInfo, CPlayerOnlineData & stPlayerOnlineData, 
                    MsgReqDrugMake & stReq, MsgAnsDrugMake & stAns);
    
    int GetPurifySurprise (unsigned char & ucSurprise);

    int GetDrugMakeInfo(CPlayerDetailInfo& stPlayerDetailInfo, CPlayerOnlineData & stPlayerOnlineData, 
                        unsigned short usRoleId, unsigned short usDrugId, MsgAnsDrugMakeInfo & stAns);

    int GetInheritInfo (CDrugData stSrcDrug, CDrugData stDstDrug, bool bUseGold,MsgInheritInfo & stInfo);
    int GetDrugInheritInfo (CPlayerDetailInfo& stPlayerDetailInfo, MsgReqDrugInherit & stReq, MsgAnsDrugInherit & stInfo);
    
    //计算增加的合成阶段和提纯阶段纯度
    void GetAddCapacity (CSingleDrug & stSrcDrug, CSingleDrug & stDstDrug, int & iSynthAdd, int & iPuriAdd);
    
    //获取传承的价格
    int GetInheritCost (CDrugData stSrcDrug, CDrugData stDstDrug, int & iSilver, int & iGold);
    
    //魔药传承
    int InheritDrug (CDrugData & stSrcDrug, CDrugData & stDstDrug, bool bCostGold);

    void CalcRoleAttr(CPlayerBaseInfo & stPlayerBaseInfo, CPlayerDetailInfo& stPlayerDetailInfo, CPlayerOnlineData & stPlayerOnlineData, unsigned short usRoleId);
    

    void GetAttrPbMsg(CPlayerAttr & stAttr, ::google::protobuf::RepeatedPtrField< ::MsgTypeValuePair > & stMsgAttr);

    int CheckBeforeInherit (CDrugData stSrcDrug, CDrugData stDstDrug, bool bCostGold);
    
private:
    void ChangeInheritDrugData (CDrugData & stSrcDrug, CDrugData & stDstDrug, bool bCostGold, int & iInheritValue);
    int GetInheritCapacity (int iSrcCapacity, int iDstCapacity);
    CDrugConfig _stDrugConfig;
    CRandomSet<MAX_PURIFY_SURPRISE_EVENT>  _stPurifyRandom;
};


#endif // __CDrugMgr_h__

